﻿using System.Collections.Generic;
using Newtonsoft.Json;
using OrkutAPILibrary.Constants;
using OrkutAPILibrary.Entities;

namespace OrkutAPILibrary.Methods
{
    /// <summary>
    /// Orkut Album Factory
    /// </summary>
    static class OrkutAlbumFactory
    {
        const Formatting Formatting = new Formatting();

        /// <summary>
        /// Gets the self albums.
        /// </summary>
        /// <param name="startIndex">The start index.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutAlbum> GetSelfAlbums(int startIndex = 0, int count = OrkutLibraryConstants.DefaultNoOfAlbums)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "albums.get"},
                                    {"userId", "@me"},
                                    {"groupId", "@self"},
                                    {"count", count.ToString()},
                                    {"startIndex", startIndex.ToString()},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutAlbum>>(requestResponse);

        }

        /// <summary>
        /// Gets the albums.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="startIndex">The start index.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutAlbum> GetAlbums(string userId, int startIndex = 0, int count = OrkutLibraryConstants.DefaultNoOfAlbums)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "albums.get"},
                                    {"userId", userId},
                                    {"groupId", "@self"},
                                    {"count", count.ToString()},
                                    {"startIndex", startIndex.ToString()},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutAlbum>>(requestResponse);
        }

        /// <summary>
        /// Updates the album.
        /// </summary>
        /// <param name="albumToUpdate">The album to update.</param>
        /// <returns></returns>
        internal static bool UpdateAlbum(OrkutAlbum albumToUpdate)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "albums.update"},
                                    {"userId", "@me"},
                                    {"groupId", "@self"},
                                    {"albumId", albumToUpdate.Id},
                                    {"album", JsonConvert.SerializeObject(albumToUpdate)}
                                };
            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse) ? true : false;
        }

        /// <summary>
        /// Creates the album.
        /// </summary>
        /// <param name="albumTitle">The album title.</param>
        /// <param name="albumDescription">The album description.</param>
        /// <returns></returns>
        internal static OrkutAlbum CreateAlbum(string albumTitle, string albumDescription)
        {
            var albumToCreate = new OrkutAlbum
            {
                Title = albumTitle,
                Description = albumDescription,
            };

            var set = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "albums.create"},
                                    {"userId", "@me"},
                                    {"groupId", "@self"},
                                    {"album", JsonConvert.SerializeObject(albumToCreate,Formatting, set)}
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutAlbum>(requestResponse);
        }

        /// <summary>
        /// Deletes the album.
        /// </summary>
        /// <param name="albumId">The album id.</param>
        /// <returns></returns>
        internal static bool DeleteAlbum(string albumId)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "albums.delete"},
                                    {"userId", "@me"},
                                    {"groupId", "@self"},
                                    {"albumId", albumId}
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse) ? true : false;
        }
    }
}
